<?php

function report_global_komponen($karyawan_id,$periode_id){
	$query = "	SELECT  komponen_id , kalkulasi FROM pr_gaji_komponen   
				WHERE  karyawan_id = {$karyawan_id} AND  periode_id = {$periode_id}";
	$result = my_query($query);
	$datas = array();
	while( $ey = my_fetch_array($result) ){
		$datas[$ey['komponen_id']]= $ey['kalkulasi'];
	}
	return $datas;
}


function report_pr_komponen_gaji( $periode_id){
	$header = array(
		'NIK'=>array(),  
		'CCC'=>array(),  
		'Account'=>array(),  
		'Description'=>array(), 
		'Nominal'=>array(),   
	);
	
	$query = "select b.nik,a.karyawan_id, a.gapok_km	,a.gapok_ks	,a.gaji_dibayar	, a.pajak	, a.takehomepay from pr_gaji a
	INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
	WHERE   a.periode_id = {$periode_id} ORDER BY b.nik ASC ";

	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$datas_komponens =   report_global_komponen($ey['karyawan_id'] , $periode_id); 
	
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan 	= loaddata_jabatan($karyawan['jabatan_id']);
		$ccc = my_get_data_by_id('pos_biaya' ,'pos_biaya_id' , $jabatan['pos_biaya_id']);
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),    
			'ccc'=>  $ccc['code'],     
			'acount'=>  "42101",    
			'nama'=>strtoupper('GAJI KM'),
			'baru'=>  $ey['gapok_km'] 
		);
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),    
			'ccc'=>  $ccc['code'], 
			'acount'=>  "42101",       
			'nama'=>strtoupper('GAJI DIBAYARKAN'),
			'baru'=>  $ey['gaji_dibayar'] 
		);
		
		foreach($datas_komponens as $komponen_id => $kalkulasi){

			$nama_komponen = get_komponen_name($komponen_id);
			if($kalkulasi < 0)$kalkulasi = (-1) * $kalkulasi;
			
			$komponen 	= my_get_data_by_id('pr_komponen_gaji' , 'komponen_gaji_id', $komponen_id );
			$account 	= my_get_data_by_id('jurnal_gaji' ,'jurnal_gaji_id' , $komponen['jurnal_gaji_id']);
			$row[] = array( 
				'nik'=>  "'".sprintf('%07d',$ey['nik']),     
				'ccc'=>  $ccc['code'],    
				'acount'=>  $account['kode_jurnal_gaji'] ,      
				'nama'=> $nama_komponen,
				'baru'=>  $kalkulasi  
			);
		}
		/*
		$row[] = array( 
			'nik'=>  "'".sprintf('%07d',$ey['nik']),    
			'nama'=>strtoupper('TAKE HOME PAY'),
			'baru'=>  $ey['takehomepay'] 
		); 
		*/		
		
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,3 ,false ); 	
}

function get_komponen_name($komponen_id){
	$query = "SELECT nama_komponen_gaji FROM pr_komponen_gaji WHERE komponen_gaji_id = {$komponen_id}";
	$result =my_query($query);
	$row = my_fetch_array($result);
	return strtoupper($row['nama_komponen_gaji']);
} 

function get_nominal_pajak($karyawan_id,$periode_id){
	$query = "SELECT pajak_23 FROM pr_gaji_pajak WHERE karyawan_id = {$karyawan_id} AND periode_id = {$periode_id}";
	$result =my_query($query);
	$row = my_fetch_array($result);
	return $row['pajak_23'];
}

function form_set_download(){
	$view  = form_header( "..." , "..."  );
	$query_check_periode = " SELECT * FROM pr_periode WHERE periode_id > 23 ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('Y-m',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
	);
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Periode Gaji"   );
	
		$submit = array(
		'value' => (  ' Download '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display(  $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( ); 
	return $view;
}